package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;

import java.io.*;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by Student on 18.08.2014.
 */
public class BankService {
    public static void addClient(Bank bank, Client client) throws ClientExistsException {
        bank.addClient(client);

    }

    public static Collection<Client> getClients(Bank bank) {
        return bank.getClients();
    }

    public static void printMaximumAmountToWithdraw(Bank bank) {
        System.out.println("Maximum to withdraw:");
        for (Client client : bank.getClients()) {
            System.out.println(client.getAccounts().maximumAmountToWithdraw());
        }
    }

    public static void saveBank(Bank bank, File feedFile){
        BufferedWriter output=null;
        try {

            output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(feedFile)));
            for (Client client: bank.getClients()) {
                StringBuilder clientStr = new StringBuilder();
                Account account=client.getAccounts();
                clientStr.append("account type=").append(account.getAccountType())
                        .append(";balance=").append(account.getBalance())
                        .append(";overdraft=").append(account.getAccountType().equals("s")?"0":((CheckingAccount)account).getOverdraft())
                        .append(";name=").append(client.getName())
                        .append(";gender=").append(client.getGender().equals(Gender.MALE)?"m":"f");
                output.write(new String(clientStr));
                output.newLine();
            }

        }catch (IOException e){
            e.printStackTrace();
        }        finally {
            try {
                if (output != null) output.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }

        }


    }
    public static Bank readBank(File feedFile,Bank bank){

        BufferedReader input=null;
        try {

            input = new BufferedReader(new InputStreamReader((new FileInputStream(feedFile))));
            String str = null;
            while ((str = input.readLine())!= null){
                StringTokenizer tokenizer = new StringTokenizer(str,";");
                ClientBuilder clientBuilder = new ClientBuilder();

                StringTokenizer keyVal = new StringTokenizer(tokenizer.nextToken(), "=");
                keyVal.nextToken();
                clientBuilder.withAccountType(keyVal.nextToken());

                keyVal = new StringTokenizer(tokenizer.nextToken(), "=");
                keyVal.nextToken();
                clientBuilder.withBalance(new Double(keyVal.nextToken()));

                keyVal = new StringTokenizer(tokenizer.nextToken(), "=");
                keyVal.nextToken();
                clientBuilder.withOverdraft(new Double(keyVal.nextToken()));

                keyVal = new StringTokenizer(tokenizer.nextToken(), "=");
                keyVal.nextToken();
                clientBuilder.withName(keyVal.nextToken());

                keyVal = new StringTokenizer(tokenizer.nextToken(), "=");
                keyVal.nextToken();
                Gender gender = ("m".equals(keyVal.nextToken())) ? Gender.MALE : Gender.FEMALE;
                clientBuilder.withGender(gender);

                bank.addClient(clientBuilder.build());
           }


        }catch (IOException e){
            e.printStackTrace();
        } catch (ClientExistsException e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null) input.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }

        }
        return bank;

    }


}

